<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\adminapi\controller;

use app\adminapi\model\Api;
use app\api\model\JWT;
use think\App;
use think\Controller;
use think\Db;

/**
 * @title 后台财务管理
 * @description 接口说明
 */
class Finance extends Api
{
    /**
     * @title 财务管理首页
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/Finance/index
     * @method POST
     * @param name:page type:int require:1 default: other: desc:页码
     * @param name:search type:int require:0 default: other: desc:搜索信息
     * @param name:begin_time type:int require:0 default: other: desc:开始时间
     * @param name:end_time type:int require:0 default: other: desc:结束时间
     * @param name:status type:int require:0 default: other: desc:状态1未结单2已结单
     * @return page:总页数
     */
    public function index()
    {
        $user = $this->user;
        $page = input('page')??1;
        $begin_time = input('begin_time');
        $end_time = input('end_time');
        $search = input('search');
        $status = input('status');
        if(!$begin_time && !$end_time){
            $date = date("Y-m-d");
            // 本月第一天
            $first = date('Y-m-01', strtotime($date));
            // 本月最后一天
            $last = date('Y-m-d', strtotime("$first +1 month -1 day"));

            $str = explode("-",$first);
            $year = $str[0];
            $month = $str[1];
            $day = $str[2];

            $strs = explode("-",$last);
            $years = $strs[0];
            $months = $strs[1];
            $days = $strs[2];

            $where[] = ['begin_year','between',[$year,$years]];
            $where[] = ['begin_month','between',[$month,$months]];
            $where[] = ['begin_day','between',[$day,$days]];
        }else{
            $str = explode("-",$begin_time);
            $year = $str[0];
            $month = $str[1];
            $day = $str[2];

            $strs = explode("-",$end_time);
            $years = $strs[0];
            $months = $strs[1];
            $days = $strs[2];
            
            $where[] = ['begin_year','between',[$year,$years]];
            $where[] = ['begin_month','between',[$month,$months]];
            $where[] = ['begin_day','between',[$day,$days]];
        }
        if(is_numeric($search)){
            $where[] = ['book','like',"%".$search."%"];
        }else{
            $where[] = ['mobile','like',"%".$search."%"];
        }
        if($status == 1){
            $where[] = ['type','=',2];
        }elseif($status == 2){
            $where[] = ['type','=',3];
        }else{
            $where[] = ['type','neq',1];
        }
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_del','=',2];
        $list = db::name('scheduled')->where($where)
        ->field('id,book,addtime,begin_year,begin_month,begin_day,t_name,b_name,status')
        ->order('addtime desc')->page("$page,10")->select();
        $total = db::name('scheduled')->where($where)->count();
        foreach ($list as $k => $v) {
            $list[$k]['begin'] = $v['begin_year'] . '-' .$v['begin_month'] . '-' .$v['begin_day'];
            unset($list[$k]['begin_year']);
            unset($list[$k]['begin_month']);
            unset($list[$k]['begin_day']);
            $list[$k]['addtime'] =  date('Y-m-d',$v['addtime']);
            if($v['status'] == 1){
                $list[$k]['receipt'] = '';
                // 尾款
                $list[$k]['payment'] = '';
                // 总额
                $list[$k]['cou_money'] = '';
            }elseif($v['status'] == 2){
                $wheres[] = ['sid','=',$v['id']];
                $wheres[] = ['status','=',2];
                // 预收
                $scheduled_deposit = db::name('scheduled_deposit')->where($wheres)->sum('money');
                $list[$k]['receipt'] = $scheduled_deposit;
                // 尾款
                $list[$k]['payment'] = '';
                // 总额
                $list[$k]['cou_money'] = '';
            }else{
                $wheres[] = ['sid','=',$v['id']];
                // 预收
                $check_order = db::name('check_order')->where($wheres)->field('total_money,paid,tail')->find();
                $list[$k]['receipt'] = $check_order['paid'];
                // 尾款
                $list[$k]['payment'] = $check_order['tail'];
                // 总额
                $list[$k]['cou_money'] = $check_order['total_money'];
            }
        }
        $data['list'] = $list;
        $data['page'] = ceil($total/10);
        $data['pages'] = 10;
        $data['total'] = $total;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 待审核详情
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/Finance/receipt
     * @method POST
     * @param name:id type:int require:0 default: other: desc:订单id
     * 
     * @return list:列表@
     * @list id:待审核id num:编号 image:收据 money:金额 type_name:预收类型 pay_name:支付方式 type:1预付款2担保人
     * @return guarantee:担保人@
     * @guarantee id:待审核id gid:担保人
     */
    public function receipt()
    {
        $user = $this->user;
        $id = input('id');
        if(!$id){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $where[] = ['sid','=',$id];
        $where[] = ['status','=',1];
        $scheduled_deposit = db::name('scheduled_deposit')->where($where)->field('id,num,image,money,type_name,pay_name,addtime')->select();
        if($scheduled_deposit){
            foreach ($scheduled_deposit as $k => $v) {
                if($v['image']){
                    $scheduled_deposit[$k]['image'] = get_file_path($v['image']);
                }
                $scheduled_deposit[$k]['type'] = 1;
                $scheduled_deposit[$k]['addtime'] =  date("Y-m-d H:i:s",$v['addtime']);

            }
        }
        $guarantee_order =  db::name('guarantee_order')->where('did',$id)->where('status',1)->field('id,gid,addtime')->select();
        if($guarantee_order){
            foreach ($guarantee_order as $ks => $vs) {
                if($vs['gid']){
                    $guarantee_order[$ks]['gid'] = db::name('guarantee')->where('id',$vs['gid'])->value('name');
                }
                $guarantee_order[$ks]['type'] = 2;
                $guarantee_order[$ks]['addtime'] =  date("Y-m-d H:i:s",$vs['addtime']);

            }
        }

        $newArrs = array_merge($scheduled_deposit,$guarantee_order);
        $cmf_arr = array_column($newArrs, 'addtime');
        array_multisort($cmf_arr, SORT_DESC, $newArrs);

        $data['list'] = $newArrs;
        // $data['guarantee'] = $guarantee_order;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 待审核通过
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/Finance/receipt_change
     * @method POST
     * @param name:id type:int require:0 default: other: desc:待审核id
     * 
     */
    public function receipt_change()
    {
        $user = $this->user;
        $id = input('id');
        $type = input('type');
        if(!$id){
            return $this->ajaxReturn(1,'缺少参数');
        }
        if($type == 1){
            $order = db::name('scheduled_deposit')->where('id',$id)->find();
            if($order){
                db::name('scheduled_deposit')->where('id',$id)->update(['status'=>2]);
                db::name('scheduled')->where('id',$order['sid'])->update(['status'=>2]);
            }
        }else{
            $guarantee = db::name('guarantee_order')->where('id',$id)->find();
            if($guarantee){
                db::name('guarantee_order')->where('id',$id)->update(['status'=>2]);
                db::name('scheduled')->where('id',$guarantee['did'])->update(['status'=>2]);
            }
        }
        
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 结单总额
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/Finance/cou_money
     * @method POST
     * @param name:id type:int require:0 default: other: desc:订单id
     * 
     * @return list:列表@
     * @list money:金额 num:编号 image:收据 pay_name:支付方式 type:1预定2担保人 gid:担保人信息（为担保人时显示）
     * @return check:明细@
     * @check paid:预收 tail:尾款 discount:优惠 total_money:结单总额
     * 
     */
    public function cou_money()
    {
        $user = $this->user;
        $id = input('id');
        if(!$id){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $where[] = ['sid','=',$id];
        $where[] = ['status','=',2];

        $deposit = db::name('scheduled_deposit')->where($where)->field('money,num,image,pay_name,addtime')->select();
        if($deposit){
            foreach ($deposit as $k => $v) {
                if($v['image']){
                    $deposit[$k]['image'] = get_file_path($v['image']);
                    $deposit[$k]['type'] = 1;
                    $deposit[$k]['addtime'] =  date("Y-m-d H:i:s",$v['addtime']);

                }
            }
        }
        $wheres[] = ['did','=',$id];
        $wheres[] = ['status','=',2];
        $guarantee_order = db::name('guarantee_order')->where($wheres)->field('addtime,gid')->select();
        if($guarantee_order){
            foreach ($guarantee_order as $ks => $vs) {
                if($vs['gid']){
                    $guarantee_order[$ks]['gid'] = db::name('guarantee')->where('id',$vs['gid'])->value('name');
                    $guarantee_order[$ks]['type'] = 2;
                    $guarantee_order[$ks]['addtime'] =  date("Y-m-d H:i:s",$vs['addtime']);

                }
            }
        }
        $newArrs = array_merge($deposit,$guarantee_order);
        $cmf_arr = array_column($newArrs, 'addtime');
        array_multisort($cmf_arr, SORT_DESC, $newArrs);

        $check = db::name('check_order')->where('sid',$id)->field('paid,tail,discount,total_money')->find();
        $data['list'] = $newArrs;
        $data['check'] = $check;
        return $this->ajaxReturn(0,'成功',$data);
    }


}